<?php

class nlValidatorNumberRange extends sfValidatorBase {

  /**
   * Configures the current validator.
   *
   * Available options:
   *
   *  * from:   The from validator
   *  * to:     The to validator
   *  * from_field:  The name of the "from" field (optional, default: from)
   *  * to_field:    The name of the "to" field (optional, default: to)
   *
   * @param array $options    An array of options
   * @param array $messages   An array of error messages
   *
   * @see sfValidatorBase
   */
  protected function configure($options = array(), $messages = array())
  {
    $this->addMessage('invalid', 'Число "от" должно быть меньше, чем число "до"');

    $this->addOption('from', new nlValidatorNumber(array('required' => false)));
    $this->addOption('to', new nlValidatorNumber(array('required' => false)));
    $this->addOption('from_field', 'from');
    $this->addOption('to_field', 'to');

  }

  protected function doClean($value) {
    $fromField = $this->getOption('from_field');
    $toField   = $this->getOption('to_field');

    $value[$fromField] = $this->getOption('from')->clean(isset($value[$fromField]) ? $value[$fromField] : null);
    $value[$toField]   = $this->getOption('to')->clean(isset($value[$toField]) ? $value[$toField] : null);

    if ($value[$fromField] && $value[$toField])
    {
      $v = new sfValidatorSchemaCompare($fromField, sfValidatorSchemaCompare::LESS_THAN_EQUAL, $toField, array('throw_global_error' => true), array('invalid' => $this->getMessage('invalid')));
      $v->clean($value);
    }

    return $value;
  }
}
